package org.yaml_editor.editors;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.rules.*;

public class YAMLPartitionScanner extends BufferedRuleBasedScanner implements IPartitionTokenScanner {
	public final static String YAML_COMMENT = "__yaml_comment";
	public final static String YAML_TAG = "__yaml_tag";
	public final static String YAML_KEY = "__yaml_key";
	
	/** The content type of the partition in which to resume scanning. */
	protected String fContentType;

	/** The offset of the partition inside which to resume. */
	protected int fPartitionOffset;

	public YAMLPartitionScanner() {

		IToken yamlComment = new Token(YAML_COMMENT);
		IToken tag = new Token(YAML_TAG);
		IToken key = new Token(YAML_KEY);

		//List<IPredicateRule> rules = new ArrayList<IPredicateRule>();
		Vector<IRule> rules = new Vector<IRule>();

		// comments
		rules.add(new SingleLineRule("---", null, yamlComment));
		rules.add(new SingleLineRule("#", null, yamlComment));
		
		// tag
		rules.add(new SingleLineRule("\"", "\"", tag));
		rules.add(new SingleLineRule("\'", "\'", tag));
		
		rules.add(new TagRule(key));

        IRule[] r = new IRule[1];
        setRules(rules.toArray(r));
		
	}

	public void setPartialRange(IDocument document, int offset, int length, String contentType, int partitionOffset) {
		fContentType = contentType;
		fPartitionOffset = partitionOffset;
		if (partitionOffset > -1) {
			int delta = offset - partitionOffset;
			if (delta > 0) {
				super.setRange(document, partitionOffset, length + delta);
				fOffset = offset;
				return;
			}
		}
		super.setRange(document, offset, length);
	}
}
